﻿using DeviceManager.Login;
using Sails.Log;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Threading;

namespace DeviceManager
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /*
        [STAThread]
        static void Main()
        {
            try
            {
                if (DB.ManagerDBCtrl.GetMng() != null)
                {
                    // 方式1
                    Application app = new Application();
                    app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    try
                    {
                        
                            MainWindow win = new MainWindow();
                            app.Run(win);
                    }
                    catch  { }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }*/

       

        /// <summary>
        /// 应用程序启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            
            Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }

        /// <summary>
        /// UI线程抛出全局异常事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
               LogManager.Error(e.Exception.Message);
                e.Handled = true;
            }
            catch (Exception ex)
            {
                LogManager.Error(e.Exception.Message);
                MessageBox.Show("应用程序发生不可恢复的异常，将要退出！");
            }
        }

        /// <summary>
        /// 非UI线程抛出全局异常事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                    LogManager.Error(exception.Message);
                }
            }
            catch (Exception ex)
            {
                LogManager.Error(ex.Message);
                MessageBox.Show("应用程序发生不可恢复的异常，将要退出！");
            }
        }
    }
}
